package com.longxiao.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.longxiao.entity.TeacherLecture;
import com.longxiao.enums.UserType;
import com.longxiao.mapper.TeacherLectureMapper;
import com.longxiao.vo.TchUserVo;
import com.longxiao.vo.UserVo;

@Service("teacherLectureService")
public class TeacherLectureService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Resource
	private UserService userService;
	@Resource
	private TeacherLectureMapper teacherLectureMapper;
	
	/**
	 * 为老师支付课时费用，修改已支付课时数
	 * @param tchId
	 * @param lectures
	 * @return
	 * @throws LXException
	 */
	@Transactional
	public boolean payLecture(String tchId,int lectures) throws Exception {
		logger.info("为老师支付费用，tchId:{},lectures:{}",tchId,lectures);
		UserVo userVo = userService.getUserInfo(tchId);
		if (!userVo.getUserType().equals(UserType.Tch.getCode())) {
			throw new Exception("该用户不是老师，无法完成操作！");
		}
		
		TeacherLecture teacherLecture = teacherLectureMapper.selectByUserId(tchId);
		int totalLecture = teacherLecture.getTeachedLectures();
		int payLecture = teacherLecture.getPaiedLectures();
		if(payLecture + lectures > totalLecture) {
			throw new Exception("支付课时数超过总课时数，无法支付！");
		}
		teacherLecture.setPaiedLectures(payLecture + lectures);
		return teacherLectureMapper.updateByPrimaryKeySelective(teacherLecture) > 0;
	}
	
	public List<TchUserVo> getTchUserVoList(TchUserVo tchUser,int offset,int rows) {
		return teacherLectureMapper.getTchUserVoList(tchUser, offset, rows);
	}
	
	public int countTchUserVoList(TchUserVo tchUser) {
		return teacherLectureMapper.countTchUserVoList(tchUser);
	}
}
